using ThoughtWorks.CruiseControl.Core.Tasks.Conditions;
using Exortech.NetReflector;
using NUnit.Framework;

namespace ThoughtWorks.CruiseControl.UnitTests.Core.Tasks.Conditions
{
    [TestFixture]
    public class andTaskConditionTest
    {
        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
        }

        [TestFixtureTearDown]
        public void TestFixtureTearDown()
        {
        }

        [Test]
        [Description("XmlInterface")]
        [Category("passing test")]
        public void XmlInterface()
        {
            string testXml = "<andCondition><conditions><mock result=\"true\"/><mock result=\"false\"/></conditions></andCondition>";
            
            NetReflectorTypeTable table = new NetReflectorTypeTable();
            table.Add(typeof(AndTaskCondition));
            table.Add(typeof(TaskConditionMock));
            AndTaskCondition result = NetReflector.Read(testXml, table) as AndTaskCondition;

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Conditions.Length, Is.EqualTo(2));
            Assert.That(result.Conditions[0].Eval(null), Is.EqualTo(true));
            Assert.That(result.Conditions[1].Eval(null), Is.EqualTo(false));
            Assert.That(result.Eval(null), Is.EqualTo(false));
        }


        [Test]
        [Description("BulkTest Pass")]
        [Category("Bulk Test")]
        public void andTaskConditionTest_BulkTest_Pass()
        {
            executeTest(true, true, true);
            executeTest(true, false, false);
            executeTest(false, true, false);
            executeTest(false, false, false);
        }

        private void executeTest(bool conditionResult1, bool conditionResult2, bool expected)
        {
            bool result = buildTestObject(conditionResult1, conditionResult2).Eval(null);
            string message = string.Format("Compared '{0}' to '{1}' and expected '{2}'",conditionResult1,conditionResult2,expected);
            Assert.That(result, Is.EqualTo(expected),message);
        }

        private AndTaskCondition buildTestObject(params bool[] results)
        {
            AndTaskCondition result = new AndTaskCondition();

            result.Conditions = TaskConditionMock.GetMockConditions(results);

            return result;
        }

        [Test]
        [Description("emptyConditions EvalsToFalse")]
        [Category("Category")]
        [ExpectedException(typeof(System.NullReferenceException))]
        public void andTaskConditionTest_emptyConditions_EvalsToFalse()
        {
            AndTaskCondition taskCondition = new AndTaskCondition();
            bool condition = taskCondition.Eval(null);
            Assert.That(condition, Is.False);
        }
    }
}